#include "load.h"
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"  
#include "rapidxml/rapidxml_print.hpp"  

using namespace rapidxml;

#ifdef WIN32

#include <Windows.h>

void CLoader::MBCS2Unicode(char *szIn, int szOutLen, wchar_t *szOut)
{
	const size_t len = szOutLen / sizeof(wchar_t);
	int res = MultiByteToWideChar(CP_ACP, 0, szIn, -1, szOut, len);

	if (res == 0)
	{
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
		{
			szOut[len - 1] = 0;
		}
		else
		{
			szOut[0] = 0;
		}
	}
}

void CLoader::Unicode2MBCS(wchar_t *szIn, int szOutLen, char *szOut)
{
	int res = WideCharToMultiByte(CP_ACP, 0, szIn, -1, szOut, szOutLen, NULL, NULL);

	if (0 == res)
	{
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
		{
			szOut[szOutLen - 1] = 0;
		}
		else
		{
			szOut[0] = 0;
		}
	}
}

#endif

void CLoader::Load()
{
	file<> fdoc(CFG_XML);
	xml_document<>   doc;
	doc.parse<0>(fdoc.data());
	xml_node<>* root = doc.first_node("QuoteSvr");
	xml_attribute<char> * sz = 0;
	xml_node<>* node = 0;

	//ctp
	node = root->first_node("Server");
	sz = node->first_attribute("Quote");
	sprintf(m_cfg.szQuoteAddr, "%s", sz->value());
	sz = node->first_attribute("BrokerID");
	sprintf(m_cfg.szBrokerID, "%s", sz->value());
	sz = node->first_attribute("AccountID");
	sprintf(m_cfg.szAccount, "%s", sz->value());
	sz = node->first_attribute("Password");
	sprintf(m_cfg.szPwd, "%s", sz->value());
	
	//ins
	node = root->first_node("Instrument");
	node = node->first_node("Ins");
	int i = 0;
	while (node && i < MAX_SUB)
	{
		sz = node->first_attribute("code");
		sprintf(m_cfg.stIns[i].szIns, "%s", sz->value());
		++i;
		node = node->next_sibling("Ins");
	}
}
